/*
 *  ScalaSwingSet
 *  Copyright (c) 2008 Andy Hicks. All rights reserved.
 *  http://www.scalalabs.net
 *
 *  This software is released under the terms of the Revised BSD License.
 *  There is NO WARRANTY. See the file LICENSE for the full text.
 */

package com.scalalabs.scalaswingset.utils

import scala.swing.{Panel, SequentialContainer, BorderPanel}
import java.awt.image.BufferedImage
import java.awt.{Graphics2D, Image, Color, GradientPaint}


class GradientPanel( from: Color, to: Color) extends Panel with SequentialContainer.Wrapper {
  opaque = false
  background = Color.black
  var fromColor = from;
  var toColor = to;

  def this() = this( Color.white, Color.black)

//  var image:Image

//  def fromColor(c: Color ) = { to = c }
//  def toColor(c: Color ) = { to = c }


  override def paintComponent(g: java.awt.Graphics) {
    g.drawImage(gradientImage(), 0, 0, null)
    super.paintComponent(g)
  }

  def gradientImage():Image = {
//    if( image != null && (size.width != image.width) && (size.height != image.height)) {
      createGradientImage()
//    }
  }

  def createGradientImage():Image =  {
    val gradientImage = createCompatibleImage()
    val gradient = new GradientPaint(0, 0, fromColor, 0, size.height, toColor, false)
    val g2 = gradientImage.getGraphics().asInstanceOf[Graphics2D]
    g2.setPaint(gradient);
    g2.fillRect(0, 0, size.width, size.height)
    g2.dispose()

    gradientImage
  }


  def createCompatibleImage(): BufferedImage =  {
    java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().
            createCompatibleImage(size.width, size.height)

  }

}